﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using QuanLyDiem.models;
using QuanLyDiem.Controllers;
using Library.UI.Form;
namespace QuanLyDiem.DanhMuc
{
    public partial class frmSinhVien : DevExpress.XtraEditors.XtraForm
    {
        public frmSinhVien()
        {
            InitializeComponent();
        }
        public int abc { get; set; }
        private void frmSinhVien_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            gridView1.Config(true, false, false, false, true, false, true, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            gridView1.DisplayFormatColumn(colNgay_sinh, "dd/MM/yyyy", DevExpress.Utils.FormatType.DateTime);
            C_GioiTinh.LoadDataSource(rleID_gioi_tinh, db);
            C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
            gridView1.BestFitAllColumns();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
            if (sv == null) return;
            txtdiachi.Text = sv.Dia_chi_tt;
            txtlop.Text = sv.Lop;
            txtmsv.Text = sv.Ma_sv;
            dteNgaysinh.EditValue = sv.Ngay_sinh;
            txtquequan.Text = sv.Que_quan;
            txtten.Text = sv.Ho_ten;
            rdNam.Checked = sv.ID_gioi_tinh == 0 ? true : false;
            rdNu.Checked = sv.ID_gioi_tinh == 1 ? true : false;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnthem.Enabled = btnsua.Enabled = btnxoa.Enabled = false;
            txtdiachi.Enabled = txtlop.Enabled = txtmsv.Enabled = txtquequan.Enabled = txtten.Enabled = dteNgaysinh.Enabled = panelControl1.Enabled = true;
            txtdiachi.Text = txtlop.Text = txtmsv.Text = txtquequan.Text = txtten.Text = "";
            dteNgaysinh.EditValue = new DateTime(1980, 01, 01);
            rdNam.Checked = true;
            //  txtmsv.Focus();
            abc = 1;
            btnhuy.Enabled = btnthuchien.Enabled = true;
        }

        private void btnthuchien_Click(object sender, EventArgs e)
        {
            Context db = new Context();
            if (txtdiachi.Text == "" && txtlop.Text == "" && txtmsv.Text == "" && txtquequan.Text == "" && txtten.Text == "" || txtdiachi.Text == "" || txtlop.Text == "" || txtmsv.Text == "" || txtquequan.Text == "" || txtten.Text == "")
            {
                MessageBox.Show("Bạn phải nhập đủ dữ liệu");
            }
            else
            {
                if (abc == 1)
                {
                    var rs = MessageBox.Show("Bạn có muốn thêm dữ liệu không", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (rs == System.Windows.Forms.DialogResult.Yes)
                    {
                        try
                        {
                            db.Connection.Open();
                            db.Transaction = db.Connection.BeginTransaction();
                            var sv = new HoSoSinhVien()
                            {
                                Dia_chi_tt = txtdiachi.Text,
                                Ho_ten = txtten.Text,
                                Ma_sv = txtmsv.Text,
                                Que_quan = txtquequan.Text,
                                Lop = txtlop.Text,
                                ID_gioi_tinh = rdNam.Checked == true ? 0 : 1,
                                Ngay_sinh = (DateTime)dteNgaysinh.EditValue

                            };

                            db.HoSoSinhViens.InsertOnSubmit(sv);
                            db.SubmitChanges();

                            //if (!C_SinhVien.Insert(sv)) throw new Exception();

                            var id_lop = db.Lops.Where(p => p.Ten_lop == txtlop.Text).Select(o => o.ID_lop).First();
                            var id_sv = db.HoSoSinhViens.Where(p => p.Ma_sv == txtmsv.Text).Select(o => o.ID_sv).First();
                            var dslop = new DanhSachLop()
                            {
                                ID_lop = id_lop,
                                ID_sv = id_sv
                            };
                            db.DanhSachLops.InsertOnSubmit(dslop);
                            db.SubmitChanges();

                            //string insertStatement = string.Format("Insert into DanhSachLop(ID_sv,ID_lop) values({0}, {1})",id_sv,id_lop);
                            //db.ExecuteQuery<DanhSachLop>(insertStatement);

                            db.Transaction.Commit();
                            MessageBox.Show("Thêm thành công");
                            C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
                            gridView1.BestFitAllColumns();
                            btnthem.Enabled = btnsua.Enabled = btnxoa.Enabled = true;
                            txtdiachi.Enabled = txtlop.Enabled = txtmsv.Enabled = txtquequan.Enabled = txtten.Enabled = dteNgaysinh.Enabled = panelControl1.Enabled = false;
                        }
                        catch (Exception)
                        {
                            db.Transaction.Rollback();
                            MessageBox.Show("Lỗi nhập tên lớp", "Thông báo");
                        }
                    }
                }
                else if (abc == 2)
                {
                    var rs = MessageBox.Show("Bạn có muốn sửa dữ liệu không", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (rs == System.Windows.Forms.DialogResult.Yes)
                    {
                        try
                        {
                            db.Connection.Open();
                            db.Transaction = db.Connection.BeginTransaction();
                            var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
                            if (sv.Lop != txtlop.Text)
                            {
                                var dslop = db.DanhSachLops.Where(p => p.ID_sv == sv.ID_sv).First();
                                var id_lop = db.Lops.Where(p => p.Ten_lop == txtlop.Text).Select(o => o.ID_lop).First();
                                dslop.ID_lop = id_lop;
                                db.SubmitChanges();
                            }
                            var update = db.HoSoSinhViens.SingleOrDefault(a => a.ID_sv == sv.ID_sv);
                            update.Ho_ten = txtten.Text;
                            update.Dia_chi_tt = txtdiachi.Text;
                            update.Lop = txtlop.Text;
                            update.Ma_sv = txtmsv.Text;
                            update.Que_quan = txtquequan.Text;
                            update.ID_gioi_tinh = rdNam.Checked == true ? 0 : 1;
                            update.Ngay_sinh = (DateTime)dteNgaysinh.EditValue;
                            db.SubmitChanges();

                            db.Transaction.Commit();
                            //if (!C_SinhVien.Update(sv)) throw new Exception();
                            MessageBox.Show("Sửa thành công");
                            C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
                            gridView1.BestFitAllColumns();
                            txtdiachi.Enabled = txtlop.Enabled = txtmsv.Enabled = txtquequan.Enabled = txtten.Enabled = dteNgaysinh.Enabled = panelControl1.Enabled = false;
                            btnthem.Enabled = btnsua.Enabled = btnxoa.Enabled = true;

                        }
                        catch (Exception)
                        {
                            db.Transaction.Rollback();
                            MessageBox.Show("Lỗi");
                        }
                    }
                }
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            btnthem.Enabled = btnsua.Enabled = btnxoa.Enabled = false;
            txtdiachi.Enabled = txtlop.Enabled = txtmsv.Enabled = txtquequan.Enabled = txtten.Enabled = dteNgaysinh.Enabled = panelControl1.Enabled = true;
            abc = 2;
            btnhuy.Enabled = btnthuchien.Enabled = true;
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            Context db = new Context();
            if (gridView1.FocusedRowHandle < 0) return;
            var rs = MessageBox.Show("Bạn có muốn xóa dữ liệu không", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
            if (rs == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    db.Connection.Open();
                    db.Transaction = db.Connection.BeginTransaction();

                    var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
                    var dslop = db.DanhSachLops.Where(p => p.ID_sv == sv.ID_sv).First();
                    db.DanhSachLops.DeleteOnSubmit(dslop);
                    db.SubmitChanges();

                    db.HoSoSinhViens.Attach(sv);
                    db.HoSoSinhViens.DeleteOnSubmit(sv);
                    db.SubmitChanges();

                    db.Transaction.Commit();
                    //if (!C_SinhVien.Delete(sv)) throw new Exception();
                    C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
                    gridView1.BestFitAllColumns();
                }
                catch (Exception)
                {
                    db.Transaction.Rollback();
                    MessageBox.Show("Lỗi");
                }
            }
        }

        private void rdNam_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void txtmsv_Validating(object sender, CancelEventArgs e)
        {
            //if (string.IsNullOrEmpty(txtmsv.Text))
            //{
            //    MessageBox.Show("Chưa nhập dữ liệu mã sinh viên");
            //    txtmsv.SetFocus();
            //}
        }

        private void txtten_Validating(object sender, CancelEventArgs e)
        {
            //    if (string.IsNullOrEmpty(txtten.Text))
            //    {
            //        MessageBox.Show("Chưa nhập dữ liệu họ tên");
            //        txtten.SetFocus();
            //    }
        }

        private void txtlop_Validating(object sender, CancelEventArgs e)
        {
            //if (string.IsNullOrEmpty(txtlop.Text))
            //{
            //    MessageBox.Show("Chưa nhập dữ liệu lớp học");
            //    txtlop.SetFocus();
            //}
        }

        private void txtdiachi_Validating(object sender, CancelEventArgs e)
        {
            //if (string.IsNullOrEmpty(txtdiachi.Text))
            //{
            //    MessageBox.Show("Chưa nhập dữ liệu địa chỉ");
            //    txtdiachi.SetFocus();
            //}
        }

        private void txtquequan_Validating(object sender, CancelEventArgs e)
        {
            //if (string.IsNullOrEmpty(txtquequan.Text))
            //{
            //    MessageBox.Show("Chưa nhập dữ liệu quê quán");
            //    txtquequan.SetFocus();
            //}
        }

        private void dteNgaysinh_Validating(object sender, CancelEventArgs e)
        {
            //if (string.IsNullOrEmpty(dteNgaysinh.Text))
            //{
            //    MessageBox.Show("Chưa nhập dữ liệu ngày sinh");
            //    dteNgaysinh.SetFocus();
            //}
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
            if (sv == null) return;
            txtdiachi.Text = sv.Dia_chi_tt;
            txtlop.Text = sv.Lop;
            txtmsv.Text = sv.Ma_sv;
            dteNgaysinh.EditValue = sv.Ngay_sinh;
            txtquequan.Text = sv.Que_quan;
            txtten.Text = sv.Ho_ten;
            rdNam.Checked = sv.ID_gioi_tinh == 0 ? true : false;
            rdNu.Checked = sv.ID_gioi_tinh == 1 ? true : false;
            btnthem.Enabled = btnsua.Enabled = btnxoa.Enabled = true;
            txtdiachi.Enabled = txtlop.Enabled = txtmsv.Enabled = txtquequan.Enabled = txtten.Enabled = dteNgaysinh.Enabled = panelControl1.Enabled = false;
            btnhuy.Enabled = btnthuchien.Enabled = false;
        }
    }
}